Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

234
Visualizações
Display a number with two decimals to the user without changing the original precise value [javascript]

Say for example I the following decimal value:

let myDecimal = 117.049701384;

I want to show the user 117.05 but without changing the original precise value above.

Basically what I want is to mimic Excel behaviour with decimals, where it displays a number with two decimals but holds all the decimals for math operations.

The reason I want to do this is because I want to keep the UI clean of huge decimals numbers, but also keeping the math operations that I have to perform as precise as possible. I guess I can duplicate variables but that sounds cumbersome and tedious.

Is that posible in Javascript?

I am using Javascript and VueJS 2.x in this App.

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Of course, you can use method toFixed().

For example:

let myDecimal = 117.049701384;
console.log(myDecimal.toFixed(2)); // 117.05

about 4 years ago · Juan Pablo Isaza Relatório

0

use this where you return the mydecimal value back for the ui

mydecimal.tofixed(2)

about 4 years ago · Juan Pablo Isaza Relatório

0

If you want truncation with rounding then toFixed is the function
If you do not want rounding then you can always use regEx

let newVarT = (117.049701384).toFixed(2)-0
let newVarR = (117.049701384+"").match(/([\d]+\.[\d]{0,2})/)[1]-0
console.log( "toFixed", newVarT, "RegEx", newVarR );

The ending -0 converts back to a number

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda